package com.gjy.config.java.nacos;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;

/**
 * https://nacos.io/docs/v3.0/manual/user/java-sdk/usage/?spm=5238cd80.48b0b492.0.0.e7e55368sTSky1
 *
 * @author gjy
 * @version 1.0
 * @since 2025-10-04 14:15:10
 */
public class NacosDemo {

    private static final String SERVER_ADDR = "192.168.253.136:8848";
    private static final String NAMESPACE_ID = "7eef3e3d-4b9c-47ea-886a-775f0580c83b";

    public static void main(String[] args) throws NacosException {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, SERVER_ADDR);
        properties.put(PropertyKeyConst.NAMESPACE, NAMESPACE_ID);
        ConfigService config = NacosFactory.createConfigService(properties);

        StringBuilder sb = new StringBuilder();
        sb.append("datasource:")
                .append("  url: dddd")
                .append("  password: 1212");
        String content = sb.toString();
        boolean b = config.publishConfig("datasource.config", "DEFAULT_GROUP", content, ConfigType.YAML.getType());
        System.out.println("b = " + b);
    }

    private static String getConfig(ConfigService config) throws NacosException {
        return config.getConfig("redis.config", "DEFAULT_GROUP", 60);
    }

}
